package csc.daonjpa.java.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

import org.springframework.transaction.annotation.Transactional;

import csc.daonjpa.java.domain.Transactions;


public class TransactionDAO {

	 @PersistenceContext
	  private EntityManager em;
	/**
	 * add transaction to database
	 * @param transaction
	 * @return
	 */
	 @Transactional
	public boolean addTransaction(Transactions transaction) {
//		EntityManager em = TransactionManager.getEntityManager();
		em.persist(transaction);
		return true;
	}
	/**
	 * get all transaction from database
	 * @return
	 */
	 @Transactional
	public List<Transactions> getTransactions() {
//		EntityManager em = TransactionManager.getEntityManager();
		TypedQuery<Transactions> query = em.createQuery("SELECT a FROM Transactions a", Transactions.class);
		List<Transactions> transactions = null;
		transactions = query.getResultList();
		return transactions;
	}
	/**
	 * get all transaction of user logged in.
	 * @param idNumber
	 * @return
	 */
	 @Transactional
	public List<Transactions> getTransactionByAccount(Long idNumber) {
//		EntityManager em = TransactionManager.getEntityManager();
		TypedQuery<Transactions> query = em.createQuery("SELECT a FROM Transactions a WHERE a.account.number = :id", Transactions.class);
		query.setParameter("id", idNumber);
		List<Transactions> transactions = null;
		transactions = query.getResultList();
		return transactions;
	}
}
